package com.kkd.kkdapp.activity;

import java.util.ArrayList;
import java.util.List;

import com.google.gson.reflect.TypeToken;
import com.kkd.kkdapp.R;
import com.kkd.kkdapp.adapter.CardTypeAdapter;
import com.kkd.kkdapp.base.BaseActivity;
import com.kkd.kkdapp.http.entity.CardItem;
import com.kkd.kkdapp.http.manager.HttpCallBack;
import com.kkd.kkdapp.http.manager.KkdHttpMagager;
import com.kkd.kkdapp.utils.SystemUtils;

import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ListView;
import android.widget.RelativeLayout;

public class SupportbankActivity extends BaseActivity implements
		OnClickListener {

	private final static int SUCCESS = 1;
	private final static int FAILURE = 2;

	private RelativeLayout mReturn;
	private ListView mListView;
	private CardTypeAdapter mAdapternew;
	private ArrayList<CardItem> datalist = new ArrayList<CardItem>();
	private List<CardItem> cardList;
	private String desc;

	Handler mHandler = new Handler() {

		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case SUCCESS:
				datalist.addAll(cardList);
				mAdapternew.notifyDataSetChanged();
				break;
			case FAILURE:
				showToast(desc);
				break;
			}
		};

	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_supportbank);
		initView();
		setListener();
		questCardList();
	}

	public void initView() {
		mReturn = (RelativeLayout) this.findViewById(R.id.add_title_img);
		mListView = (ListView) this.findViewById(R.id.type_list);
		mAdapternew = new CardTypeAdapter(this, datalist);
		mListView.setAdapter(mAdapternew);
	}

	public void setListener() {
		mReturn.setOnClickListener(this);
	}

	public void questCardList() {
		KkdHttpMagager.getInstance().postCard(
				new TypeToken<List<CardItem>>() {
				}.getType(), new HttpCallBack() {

					@Override
					public void onSuccess(int statusCode, Object responseBody) {
						cardList = (List<CardItem>) responseBody;
						mHandler.sendEmptyMessage(SUCCESS);
					}

					@Override
					public void onFailure(int statusCode, String desc) {
						SupportbankActivity.this.desc = desc;
						mHandler.sendEmptyMessage(FAILURE);
					}
				});
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.add_title_img:
			finish();
			break;
		}

	}
}
